package stack;

import java.util.Arrays;
import java.util.NoSuchElementException;

public class Stack<E> {
    private E[] elementData;
    private int size;
    public Stack(){
        elementData= (E[]) new Object[10];
    }
    public Stack(int initCap){
        elementData= (E[]) new Object[initCap];
    }
    public void push(E value){
        elementData[size++]=value;
    }
    public E pop(){
        if(getSize()==0){
            throw new NoSuchElementException("栈为空");
        }
        E oldValue=elementData[size-1];
        size--;
        elementData[size]=null;
        return oldValue;
    }
    public E peek(){
        if(getSize()==0){
            throw new NoSuchElementException("栈为空");
        }
        return elementData[size-1];
    }
    public int getSize() {
        return size;
    }

    @Override
    public String toString() {
        StringBuilder sb=new StringBuilder();
        sb.append("[");
        for(int i=0;i<size;i++){
            sb.append(elementData[i]);
            if(i!=size-1){
                sb.append(",");
            }
        }
        sb.append("] top");
        return sb.toString();
    }

    public boolean isEmpty() {
        return true;
    }
}
